package com.infore.qualityControl.conf;

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.infore.qualityControl.quartz.ScheduledTasks;

@Configuration
public class QuartzConfig {

	// 配置中设定了
	// ① targetMethod: 指定需要定时执行scheduleInfoAction中的simpleJobTest()方法
	// ② concurrent：对于相同的JobDetail，当指定多个Trigger时, 很可能第一个job完成之前，
	// 第二个job就开始了。指定concurrent设为false，多个job不会并发运行，第二个job将不会在第一个job完成之前开始。
	// ③ cronExpression：0/10 * * * * ?表示每10秒执行一次，具体可参考附表。
	// ④ triggers：通过再添加其他的ref元素可在list中放置多个触发器。 scheduleInfoAction中的simpleJobTest()方法

	// 零点核查定时任务
	@Bean(name = "GetZeroCheckDataJSonJobDetail")
	public MethodInvokingJobDetailFactoryBean GetZeroCheckDataJSonJobDetail(ScheduledTasks scheduledTasks) {
		MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
		bean.setTargetObject(scheduledTasks);
		bean.setTargetMethod("GetZeroCheckDataJSon");
		bean.setConcurrent(false);
		return bean;
	}

	@Bean(name = "GetZeroCheckDataJSonTrigger")
	public CronTriggerFactoryBean GetZeroCheckDataJSonTrigger(JobDetail GetZeroCheckDataJSonJobDetail) {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(GetZeroCheckDataJSonJobDetail);
		try {
			tigger.setCronExpression("0 30/5 * * * ? ");// 每小时后半小时每隔5分钟执行一次
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tigger;
	}
	
	// 跨度核查定时任务
	@Bean(name = "GetSpanCheckDataJSonJobDetail")
	public MethodInvokingJobDetailFactoryBean GetSpanCheckDataJSonJobDetail(ScheduledTasks scheduledTasks) {
		MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
		bean.setTargetObject(scheduledTasks);
		bean.setTargetMethod("GetSpanCheckDataJSon");
		bean.setConcurrent(false);
		return bean;
	}

	@Bean(name = "GetSpanCheckDataJSonTrigger")
	public CronTriggerFactoryBean GetSpanCheckDataJSonTrigger(JobDetail GetSpanCheckDataJSonJobDetail) {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(GetSpanCheckDataJSonJobDetail);
		try {
			tigger.setCronExpression("0 30/5 * * * ? ");// 每小时后半小时每隔5分钟执行一次
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tigger;
	}
	
	// 核查定时任务
	@Bean(name = "GetCheckDataJSonJobDetail")
	public MethodInvokingJobDetailFactoryBean GetCheckDataJSonJobDetail(ScheduledTasks scheduledTasks) {
		MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
		bean.setTargetObject(scheduledTasks);
		bean.setTargetMethod("GetCheckDataJSon");
		bean.setConcurrent(false);
		return bean;
	}

	@Bean(name = "GetCheckDataJSonTrigger")
	public CronTriggerFactoryBean GetCheckDataJSonTrigger(JobDetail GetCheckDataJSonJobDetail) {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(GetCheckDataJSonJobDetail);
		try {
			tigger.setCronExpression("0 30/5 * * * ? ");// 每小时后半小时每隔5分钟执行一次
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tigger;
	}
	
	// 加标回收数据定时任务
	@Bean(name = "GetRecoveryDataJSonJobDetail")
	public MethodInvokingJobDetailFactoryBean GetRecoveryDataJSonJobDetail(ScheduledTasks scheduledTasks) {
		MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
		bean.setTargetObject(scheduledTasks);
		bean.setTargetMethod("GetRecoveryDataJSon");
		bean.setConcurrent(false);
		return bean;
	}

	@Bean(name = "GetRecoveryDataJSonTrigger")
	public CronTriggerFactoryBean GetRecoveryDataJSonTrigger(JobDetail GetRecoveryDataJSonJobDetail) {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(GetRecoveryDataJSonJobDetail);
		try {
			tigger.setCronExpression("0 30/5 * * * ? ");// 每小时后半小时每隔5分钟执行一次
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tigger;
	}
	
	// 五参数数据定时任务
	@Bean(name = "GetFiveParameterDataJSonJobDetail")
	public MethodInvokingJobDetailFactoryBean GetFiveParameterDataJSonJobDetail(ScheduledTasks scheduledTasks) {
		MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
		bean.setTargetObject(scheduledTasks);
		bean.setTargetMethod("GetFiveParameterDataJSon");
		bean.setConcurrent(false);
		return bean;
	}

	@Bean(name = "GetFiveParameterDataJSonTrigger")
	public CronTriggerFactoryBean GetFiveParameterDataJSonTrigger(JobDetail GetFiveParameterDataJSonJobDetail) {
		CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
		tigger.setJobDetail(GetFiveParameterDataJSonJobDetail);
		try {
			tigger.setCronExpression("0 30/5 * * * ? ");// 每小时后半小时每隔5分钟执行一次
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tigger;
	}
	
	@Bean
	public SchedulerFactoryBean schedulerFactory(Trigger GetZeroCheckDataJSonTrigger,
			Trigger GetSpanCheckDataJSonTrigger, Trigger GetCheckDataJSonTrigger, Trigger GetRecoveryDataJSonTrigger,
			Trigger GetFiveParameterDataJSonTrigger) {
		SchedulerFactoryBean bean = new SchedulerFactoryBean();
		bean.setTriggers(GetZeroCheckDataJSonTrigger, GetSpanCheckDataJSonTrigger, GetCheckDataJSonTrigger,
				GetRecoveryDataJSonTrigger, GetFiveParameterDataJSonTrigger);
		return bean;
	}
}
